﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace CommonLibrary.Common
{
    public class ZTreeHelper
    {
        /**  ztree 前台 json 格式
       
                         var zTreeNodes = [{
                         "id":1,
                         "name":"我的好友",
                         "nodes":[{
                                        "id":11,
                                        "name":"08001",
                                        "checked":true
                                     },{
                                        "id":12,
                                        "name":"08002"
                                        "checked":true
                                    },{
                                       "id":13,
                                       "name":"08003",
                                       "checked":true
                                   }]
                          }];
         
     **/

        static string NodeString_NoChild = "  id:{0},name:'{1}',nocheck:{2},checked:{3}";
        static string NodeString_HaveChild = " id:{0},name:'{1}',nocheck:{2},checked:{3},children:{4} ";
        public static string Serialize(TreeNode[] nodes)
        {

            string result = "";
            //根节点循环
            for (int i = 0; i < nodes.Length; i++)
            {

                //如果是叶节点
                if (nodes[i].ChildNodes.Count == 0)
                {
                    if (nodes.Length > 1) //如果该层有多个节点
                    {
                        if (i == 0)//如果是第一个节点
                        {
                            result += " [{ " + string.Format(NodeString_NoChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].Checked) + " } ";
                        }
                        else if (i == nodes.Length - 1)//如果是第最后一个节点
                        {
                            result += " ,{ " + string.Format(NodeString_NoChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].Checked) + " }] ";
                        }
                        else  //中间几点
                        {
                            result += " ,{ " + string.Format(NodeString_NoChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].Checked) + " } ";
                        }
                    }
                    else
                    {
                        result += " [{ " + string.Format(NodeString_NoChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].Checked) + " }] ";
                    }
                }
                //else if (nodes[i].ChildNodes.Count == 1)
                else
                {
                    TreeNode[] childnodes = new TreeNode[nodes[i].ChildNodes.Count];
                    nodes[i].ChildNodes.CopyTo(childnodes, 0);

                    if (nodes.Length > 1) //如果该层有多个节点
                    {
                        if (i == 0)//如果是第一个节点
                        {
                            result += " [{ " + string.Format(NodeString_HaveChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].Checked, Serialize(childnodes)) + " } ";
                        }
                        else if (i == nodes.Length - 1)//如果是第最后一个节点
                        {
                            result += " ,{ " + string.Format(NodeString_HaveChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].Checked, Serialize(childnodes)) + " }] ";
                        }
                        else  //中间几点
                        {
                            result += " ,{ " + string.Format(NodeString_HaveChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].Checked, Serialize(childnodes)) + " } ";
                        }
                    }
                    else
                    {
                        result += " [{ " + string.Format(NodeString_HaveChild, nodes[i].Value, nodes[i].Text, !nodes[i].ShowCheckBox, nodes[i].Checked, Serialize(childnodes)) + " }] ";
                    }
                }
                //else
                //{
                //    TreeNode[] childnodes = new TreeNode[nodes[i].ChildNodes.Count];
                //    nodes[i].ChildNodes.CopyTo(childnodes, 0);
                //    result += " [{ " + string.Format(NodeString_HaveChild, nodes[i].Value, nodes[i].Text, Serialize(childnodes)) + " }] ";

                //}
            }


            return result.ToLower();
        }



        private static String zTreeNodes = "{{ id:'{0}', name:'{1}',nocheck:{2},checked:{3}, tag:'{4}',children:{5} }}";
        private static String zTreeNoChilds = "{{ id:'{0}', name:'{1}',nocheck:{2},checked:{3},tag:'{4}' }}";
        public static String Serialize(Node[] nodes)
        {
            var parNodes = String.Empty;
            parNodes += serialize(nodes);
            return parNodes;
        }

        //private static String serialize(Node[] nodes)
        //{
        //    var parNodes = String.Empty;
        //    for (int parIndex = 0; parIndex < nodes.Length; parIndex++)
        //    {
        //        var chiNodes = "[";
        //        var parNode = nodes[parIndex];
        //        if (parNode.Nodes.Count >= 0)
        //        {
        //            for (int chiIndex = 0; chiIndex < parNode.Nodes.Count; chiIndex++)
        //            {
        //                chiNodes += String.Format(zTreeNoChilds, parNode.Nodes[chiIndex].Value, parNode.Nodes[chiIndex].Text);
        //                if (chiIndex < parNode.Nodes.Count - 1)
        //                    chiNodes += ",";
        //                else
        //                    chiNodes += "]";
        //            }

        //            parNodes += String.Format(zTreeNodes, nodes[parIndex].Value, nodes[parIndex].Text, chiNodes);
        //        }
        //        else
        //            parNodes += String.Format(zTreeNoChilds, parNode.Value, parNode.Text);
        //        if (parIndex < nodes.Length - 1)
        //            parNodes += ",";
        //    }
        //    return parNodes;
        //}

        public static String Serialize(Node node)
        {
            var parNodes = "[";
            parNodes += String.Format(zTreeNodes, node.Value, node.Text, (!node.ShowCheckBox).ToString().ToLower(), node.IsChecked.ToString().ToLower(), node.Tag, ZTreeHelper.serialize(node.Nodes.ToArray()));
            parNodes += "]";
            return parNodes;
        }

        private static String serialize(Node[] nodes)
        {
            var parNodes = "[";
            var chiNodes = String.Empty;
            for (int index = 0; index < nodes.Length; index++)
            {
                if (nodes[index].Nodes.Count > 0)
                {
                    chiNodes += String.Format(zTreeNodes, nodes[index].Value, nodes[index].Text, (!nodes[index].ShowCheckBox).ToString().ToLower(), nodes[index].IsChecked.ToString().ToLower(), nodes[index].Tag, serialize(nodes[index].Nodes.ToArray()));
                }
                else
                    chiNodes += String.Format(zTreeNoChilds, nodes[index].Value, nodes[index].Text, (!nodes[index].ShowCheckBox).ToString().ToLower(), nodes[index].IsChecked.ToString().ToLower(), nodes[index].Tag);

                if (index < nodes.Length - 1)
                    chiNodes += ",";
            }
            parNodes += chiNodes;
            parNodes += "]";

            return parNodes;
        }
    }
}
